Skip to content
This repository was archived by the owner on Oct 6, 2026. It is now read-only.

Introduce ApiAccess for representing access scopes - #482

Merged
rezaansyed merged 1 commit into
masterfrom
api-access
Mar 18, 2021
Merged

rezaansyed merged 1 commit into
masterfrom
api-access

Conversation

@rezaansyed

@rezaansyed rezaansyed commented Mar 11, 2021 •

Copy link
Copy Markdown
Contributor

WHY are these changes introduced?

There are common operations that are used for managing access scopes in apps. Such operations include serializing, deserializing and normalizing scopes.

WHAT is this pull request doing?

This PR introduces the ApiAccess value object that will be used to perform such operations. This helps ensure that apps do not potentially write duplicate code for scopes and delegating this responsibility to the shopify_python_api.

For further reference, we made a similar move in the Shopify API ruby gem

Usage examples

Constructing ApiAccess

api_access = ApiAccess(["read_products", "write_orders"])
another_api_access = ApiAccess("read_products, write_products")

Delegating check for whether scopes are mismatched or subset of another

def scopes_match?(existing_scopes, requesting_scopes)
  expected_api_access = ApiAccess(existing_scopes)
  actual_api_access = ApiAccess(requesting_scopes)

  actual_api_access == expected_api_access
def scopes_subset?(existing_scopes, requesting_scopes)
  expected_api_access = ApiAccess(existing_scopes)
  actual_api_access = ApiAccess(requesting_scopes)

  actual_api_access.covers(expected_api_access)

Checklist

  • I have updated the CHANGELOG (if applicable)
  • I have followed the Shopify Python guide

@NabeelAhsen NabeelAhsen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Comment thread .pre-commit-config.yaml Outdated
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pre-commit/mirrors-autopep8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment has more context: #479 (comment)

@paulomarg paulomarg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread shopify/api_access.py
def __eq__(self, other):
return type(self) == type(other) and self._compressed_scopes == other._compressed_scopes

def __store_scopes(self, scopes):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity what do double leading underscores do? 😅 Is this for name mangling?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the Python convention of defining "private" methods. Although, methods in Python cannot be truly private. This is just a way to indicate that this method should not be consumed as a public interface as it is more likely to change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh okay makes sense!

@Paulinakhew Paulinakhew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love how detailed the tests are!

Comment thread shopify/api_access.py Outdated
return type(self) == type(other) and self._compressed_scopes == other._compressed_scopes

def __store_scopes(self, scopes):
sanitized_scopes = set(list(filter(None, [scope.strip() for scope in scopes])))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't want/need the contents of the set to change after we make it, we could make this a frozenset.

Also, I don't think you need to convert to list before converting to set/frozenset.

Comment thread shopify/api_access.py Outdated

def __store_scopes(self, scopes):
sanitized_scopes = set(list(filter(None, [scope.strip() for scope in scopes])))
implied_scopes = set([self.__implied_scope(scope) for scope in sanitized_scopes])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, could be frozenset if you don't want this to mutate. Also, I think you can skip the square brackets. It turns it from a list comprehension into a generator, which the set initializer should be happy to consume

Comment thread shopify/api_access.py Outdated
match = self.IMPLIED_SCOPE_RE.match(scope)
if match:
return "{unauthenticated}read_{resource}".format(
unauthenticated="" if match.group("unauthenticated") is None else match.group("unauthenticated"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use the boolean magic here:

unauthenticated=match.group('unauthenticated') or '',

@greatestape greatestape left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I left a bunch of style comments. Nothing profound. I think this PR should include docs in the README.

@rezaansyed
rezaansyed force-pushed the api-access branch 5 times, most recently from bd11e2b to 1719e98 Compare March 17, 2021 15:53
@Paulinakhew

Copy link
Copy Markdown
Contributor

Pre-commit doesn't actually reformat your code in CI so if you want to reformat stuff locally you can do this

pip install -r requirements.txt
pre-commit install
pre-commit run --all-files

Comment thread README.md Outdated
Comment on lines +184 to +221
#### Constructing an ApiAccess

```python
api_access = ApiAccess(["read_products", "write_orders"]) # List of access scopes
another_api_access = ApiAccess("read_products, write_products") # String of comma-delimited access scopes
```

#### Serializing ApiAccess

```python
api_access = ApiAccess(["read_products", "write_orders"])

access_scopes_list = list(api_access) # ["read_products", "write_orders"]
comma_delmited_access_scopes = str(api_access) # "read_products,write_orders"
```

#### Comparing ApiAccess objects

##### Checking for API access equality

```python
expected_api_access = ApiAccess(["read_products", "write_orders"])

actual_api_access = ApiAccess(["read_products", "read_orders", "write_orders"])
non_equal_api_access = ApiAccess(["read_products", "write_orders", "read_themes"])

actual_api_access == expected_api_access # True
non_equal_api_access == expected_api_access # False
```

##### Checking if ApiAccess covers the access of another

```python
superset_access = ApiAccess(["write_products", "write_orders", "read_themes"])
subset_access = ApiAccess(["read_products", "write_orders"])

superset_access.covers(subset_access) # True
```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want to have something more targeted that illustrates how a developer could leverage this code. Is there code in our POC django app that we could simplify and excerpt here instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just moved this into a separate doc as discussed.

@rezaansyed
rezaansyed force-pushed the api-access branch 2 times, most recently from 9afe1ff to 3914837 Compare March 18, 2021 02:31
@NabeelAhsen NabeelAhsen mentioned this pull request Mar 18, 2021
2 tasks done
Comment thread README.md Resolved
Comment thread docs/api-access.md Resolved
Comment thread docs/api-access.md Resolved

@greatestape greatestape left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

This branch had an error being deployed

1 failed deployment
pypi — 227a56ce Deployed Apr 9, 2024 by shopify-shipit[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants